<?php
include( getcwd() . '/application/views/div/html_head_info.php' );
include( getcwd() . '/application/views/div/header.php' );
//用户购买商品时的页面
?>

<div class="container"> 

<div id="buy">

<p>
<?php include( getcwd() . '/application/views/div/user_settings_nav.php' ); ?>
</p>

<h2>
购买明细
</h2>

<?php 
include( getcwd() . '/application/views/div/res_info.php' );
?>

<form action="/deal/do_buy" method="post" name="deal_info">
<input type="hidden" name="product_id" value="<?php echo $product_info['product_id']; ?>">
<input type="hidden" name="class_id" value="<?php echo $product_info['product_class_id']; ?>">
<input type="hidden" name="product_title" value="<?php echo $product_info['title']; ?>">
<input type="hidden" name="product_class_name_cn" value="<?php echo $product_class_info['product_class_name_cn']; ?>">
<input type="hidden" name="user_nick" value="<?php echo @$user_info['user_nick']; ?>">
<!--订单详细列表 -->
<table id="deal_detail">
	 <thead>
		 <tr>
			 <th class="span-5">服务或商品名称</th>
			 <th class="span-2">数量</th>
			 <th class="span-1"></th>
			 <th class="span-3">单价</th>
			 <th class="span-3">总价</th>
		 </tr>
	 </thead>
	 <tbody>
		 <tr>
			<td class="deal_detail_td_desc"><?php echo $product_info[ 'title' ]; ?></td>
			<td>
			        <input id="deal_detail_quantity" maxlength="7" type="text" name="quantity" value="1"/>
			</td>
			<td>X</td>
			<td id="ori_price">
                        ￥<span><?php echo $product_info[ 'price' ]; ?></span>
                        <input type="hidden" value="<?php echo $product_info[ 'price' ]; ?>" name="price"/>
			</td>
			<!--只是商品的总价-->
			<td id="sum_price">
			￥<span><?php echo $product_info[ 'price' ]; ?></span>
			<input type="hidden" value="" name="sum_price"/>
			</td>	 
		 </tr>
	 </tbody>
</table>

<!--去除优惠等值后的价格-->
<p id="show_total_price">
<b>支付总金额 : RMB <span class="total_price">0</span> 元</b>
</p>

<div class="wavy_ban">
<br />
</div>

<?php
//判断若用户还没有登录系统 则在点击购买按钮的时候会跳转
//到登录页面 ,当然 也不会显示地址等信息
if( !empty( $subject_user_info ) ){
?>
<p id="user_address">
	<h3>收货地址</h3>
<?php
//循环显示用户地址信息
if( !empty( $user_addresses ) ){ 
?>
	 <ul id="user_address_item">
<?php
foreach( $user_addresses as $no=>$address ){
?>
        <li>	 
	        <input type="radio" name="address" class="address left" value="<?php echo $address['address']; ?>"/>
	        <span class="address_detail"><?php echo $address['address']; ?></span>
	</li>
<?php
}
?>
	 </ul>
<?php
}
?>
	 <span id="add_new_address_button">使用新地址</span> 
	 <!--添加新地址-->
	 <div id="add_new_address">
		 <p>
			 <label>收货人 :</label>
			 <input type="text" class="text" maxlength="64" name="name"/>
		 </p>
		 <p id="province_city">
			 <label for="city" class="spc">省市区 :</label>
		 	 <select id="loc_province"  name="province" class="text input city"></select>
		 	 <input name="loc_province" type="hidden" />

		 	 <select id="loc_city" style="width:100px;" name="city" class="input text"></select>
		 	 <input name="loc_city" type="hidden" />

		 	 <select id="loc_town" style="width:122px;" name="town" class="input text"></select>
		 	 <input name="loc_town" type="hidden" />
		 </p>
		 <p>
			  <label>街道地址 :</label>
			  <input type="text" class="text" maxlength="64" name="address_detail"/>
		 </p>
		 <p>
			 <label>邮政编码 :</label>
			 <input type="text" class="text zip" maxlength="64" name="zip"/>
		 </p>
		 <p>
		 	 <label>电话号码 :</label>
			 <input type="text" class="text tel" maxlength="64" name="tel"/>
		 </p> 
	 </div>
</p>
<?php
if( empty( $user_addresses ) ){ 
//若还没有可用的地址信息
//则会显示出填写地址的表单
?>
<script type="text/javascript">
	$( '#add_new_address_button' ).hide();
	$( '#add_new_address' ).show();
</script>
<?php
}
?>

</form>

<p class="text_align_center">
	 <span class="buy_button">下一步</span>
</p>

<?php
}
?>

</div>

</div>

<script src="/js/libs/jquery_plugins/location/location.js"></script>
<script src="/js/libs/jquery_plugins/location/area.js"></script>
<script src="/js/address.js"></script>

<script type="text/javascript">
$(function(){
var empty_error_info = function(){
        //清除之前的错误信息
	error_info_el.empty();	 
}
var enable_buy_button = function(){
	//使能购买按钮
        $( '.buy_button' ).click( function(){
	        $( '[name=deal_info]' ).submit();
	});
}

var disable_buy_button = function(){
	$( '.buy_button' ).unbind( 'click' );
}

var refresh_price = function(){
	 var has_error = false;
	 //按下按键时改变相应的价格
	 var quantity = deal_detail_quantity_el.attr( 'value' );
	 //判断 quantity 的有效性  
	 //如果为无效输入提示用户
	 if( quantity == '0' ){
		 error_info_el.append( '<span class="error">购买数量不能为0</span>' );
		 has_error = true;
	 }
         var sum_price = FloatMul( ori_price , quantity );

	 $( '.total_price' ).html( sum_price );
	 $( '#sum_price > span' ).html( sum_price );
	 $( 'input[name=sum_price]' ).attr( 'value' , sum_price );

	 if( !has_error ){
		 enable_buy_button();
	 }else{
		 disable_buy_button();
	 }
}
//实现价格随产品个数的改变而改变
//时间应该绑定到 onfocus ,blur 上
var deal_detail_quantity_el = $( '#deal_detail_quantity' );
//得到原价
var ori_price = parseFloat( $( '#ori_price > span' ).text() );
var error_info_el = $( '.error_info' );
deal_detail_quantity_el.bind({
	'keyup' : refresh_price ,
	'focus' : empty_error_info ,
	'keydown' : empty_error_info
});
//页面加载时刷新一次价格
refresh_price();
});
</script>

<?php
include( getcwd().'/application/views/div/footer.php' );
?>

